package com.open4cn.simulator.connect.tcp.handler;

import com.open4cn.simulator.core.parser.InstructionParserChain;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @author yinxiaopeng
 * @version 1.0.0
 * @company 致景科技
 * @department 飞梭BU/研发
 * @description 指令响应分发器
 * @Email yinxiaopeng@zj.tech
 * @date 2022-05-03 16:57
 */
@ChannelHandler.Sharable
public class DispatcherHandler extends ChannelInboundHandlerAdapter {

    // 指令分析链
    private InstructionParserChain chain;

    public DispatcherHandler(InstructionParserChain chain) {
        this.chain = chain;
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        Object result = chain.filter(msg);
        ctx.channel().writeAndFlush(result);
    }
}
